package com.tim.service.Impl;

import com.tim.dao.CarStallDao;
import com.tim.dao.Impl.CarStallDaoImpl;
import com.tim.pojo.CarStall;
import com.tim.pojo.DataResult;
import com.tim.service.CarStallService;

import java.sql.SQLException;
import java.util.List;

/**
 * 作者：Tim
 * 时间：2021/4/29 12:19
 */
public class CarStallServiceImpl implements CarStallService {
    CarStallDao carStallDao = new CarStallDaoImpl();
    @Override
    public int deleteCarStall(String id) {
        try {
            int carid= Integer.parseInt(id);
            return carStallDao.deleteCarStall(carid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCarStall(CarStall carStall) {
        try {
            return carStallDao.addCarStall(carStall);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0 ;
    }

    @Override
    public CarStall selectOne(String id) {
        try {
            int carid = Integer.parseInt(id);
            return carStallDao.selectOne(carid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateCarStall(CarStall carStall) {
        try {
            return carStallDao.updateCarStall(carStall);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<CarStall> searchCarStall(String carPlot) {
        try {
            return carStallDao.searchCarStall(carPlot);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteAll(String[] ids) {
        //1、直接循环调用删除单个的方法(但是要考虑事务问题。解决方案：动态拼接Sql语句)
        for (String id : ids) {
            int count = deleteCarStall(id);
        }
        return ids.length;
    }

    @Override
    public int addAllCarStall(List<String[]> data) {
        return 0;
    }

    @Override
    public DataResult limit(String currentPage, String pageSize, String carPlot) {
        try {
            int p=1;//当前页
            int s=5; //每页条数
            if(currentPage!=null){
                p = Integer.parseInt(currentPage);
            }
            if(pageSize!=null){
                s =Integer.parseInt(pageSize);
            }
            List<CarStall> carStallList=carStallDao.limit(p,s,carPlot);
            //因为现在使用的是layui的表格渲染，所以返回到前端的数据使用要求的 1、要求code中0表示成功     2、如果开启分页那么必须要返回一个count的参数，表示总条数
            DataResult dataResult = new DataResult(0,"查询成功",carStallList, carStallDao.count(carPlot));
            return dataResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        DataResult dataResult = new DataResult(100,"数据接口错误");
        return dataResult;
    }
}
